<?php
require_once('../includes/settings.php');
require_once('../includes/functions.php');
require_once('../includes/SimpleImage.php');
deny_get_request();
deny_anonymous();

$output_dir = UPLOAD_ROOT;

function file_name($user_id){
	return 'avatar_'.$user_id.'.png';
}

function upload(){
	global $output_dir;
	if(isset($_FILES["my_file"])){
		if ($_FILES["my_file"]["error"] > 0){
			 echo "Error: " . $_FILES["file"]["error"] . "<br>";
		}else{
			$temp_file = $output_dir.$_FILES["my_file"]["name"];
			move_uploaded_file($_FILES["my_file"]["tmp_name"], $temp_file);
			$img = new SimpleImage($temp_file);
			$img->resize(64, 64)->save($output_dir.file_name(user_val('id')));
			unlink($temp_file);
			echo "头像上传成功";
		}
	}
}

if(isset($_REQUEST['type'])){
	$type=strtolower($_REQUEST['type']);
	if($type=='avatar'){
		$output_dir=$_SERVER['DOCUMENT_ROOT'].'/'.AVATAR_FOLDER.'/';
	}
	upload();
}else{
	echo "Error:  Invalid Parameter<br>";
	exit();
}
?>